<?php

/**
 * publicRegistration actions.
 *
 * @package    ldaad
 * @subpackage publicRegistration
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class publicRegistrationActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    $this->getUser()->getAttributeHolder()->clear();
    //$this->forward('publicRegistration', 'index');
  }
  
  public function executeStartRegistration()
  {
    /*
    1. clean user session
    2. go to conference selection (by default)
    */
    $this->getUser()->getAttributeHolder()->clear();

    return $this->redirect('conference/selectConference');
  }
  
  public function executeStartRegistrant()
  {
    /*
    1. go to registrant/entry
    */
    return $this->redirect('registrant/entry');
  }
  
  public function executeStartSessions()
  {
    return $this->redirect('session/availableSessions');
  }
   
  public function executeStartSpecialEvents()
  {
    return $this->redirect('specialEvent/availableSpecialEvents');
  }   
  
  public function executeConsolidateRegistration()
  {
    //CFR to various model
    
    //get conference ID
    $conferenceID = $this->getUser()->getAttribute('conferenceIDSession');
    //get conference
    $conference = ConferencePeer::retrieveByPk($conferenceID);
    $this->conference = $conference;
    
    //getRegistrant
    $registrant = $this->getUser()->getAttribute('registrantSession');
    $this->registrant = $registrant;
    
    $selectedSessions=null;
    $isFull = false;
    //get sessionS
    if ($this->getUser()->hasAttribute('sessionIDArraySession'))
    {
      $selectedSessionIDs = $this->getUser()->getAttribute('sessionIDArraySession');

      $selectedSessions = SessionPeer::retrieveByPKs($selectedSessionIDs);

      $isFull = Conference::isFullDay($conference, $selectedSessions);

      $this->sessions = $selectedSessions;
    }
    
    $selectedSpecialEvents = null;
    //get special eventS
    if ($this->getUser()->hasAttribute('specialEventIDArraySession'))
    {
      $selectedSpecialEventIDs = $this->getUser()->getAttribute('specialEventIDArraySession');
      $selectedSpecialEvents = SpecialEventPeer::retrieveByPKs($selectedSpecialEventIDs);
      $this->special_events= $selectedSpecialEvents;
    }
    //something that can be refractored later
    
    //figureout if its earlybird
    $earlybird = $conference->isEarly(date('m-d-Y'));
 
    
    $this->getUser()->setAttribute('earlySession', $earlybird);
    $this->getUser()->setAttribute('fullSession', $isFull);
    $this->getUser()->setAttribute('memberSession', $registrant->isLDAMember());
    //findout totalConferenceFee
        
    $totalConferenceFee= $conference->getTotalConferenceFee($earlybird, $registrant->isLDAMember(), $selectedSessions, $selectedSpecialEvents);
    
    //store totalConferenceFee in user sesion and $this
    $this->getUser()->setAttribute('totalConferenceFeeSession', $totalConferenceFee);
    $this->totalConferenceFee = $totalConferenceFee;
    
    
  
  }
  
  public function executeStoreRegistration()
  {
    
    return $this->redirect('registration/store');
  
  }
  
  
  public function executeStartPayment()
  { 
    /*
    1. store initial registration information (verify Payment false)
    2. prepare info for payment page
    3. GO!
    */
    
    //1. STORE
    //create new registration
    $registration = new Registration();
    
    //get conference id
    $conferenceID = $this->getUser()->getAttribute('conferenceIDSession');
        
    //get registrant information
    $registrant = $this->getUser()->getAttribute('registrantSession');
    $registrant->save();
    
    //get session IDs
    $selectedSessionIDs = $this->getUser()->getAttribute('sessionIDArraySession');
    
    //get special event IDs
    $selectedSpecialEventIDs = $this->getUser()->getAttribute('specialEventIDArraySession');
    
    //findout totalConferenceFee
    $totalConferenceFee =  $this->getUser()->getAttribute('totalConferenceFeeSession');
    
    $registration = $registration->storeInitialRegistrationInformation ($conferenceID, $registrant, $selectedSessionIDs, $selectedSpecialEventIDs, $totalConferenceFee);
    
    //CFR
    $invoiceNumber = $registration->generateInvoiceNumber($registration);

    $registration->setInvoice($invoiceNumber);
    $registration->setIsearly($this->getUser()->getAttribute('earlySession'));
    $registration->setIsdaily(!$this->getUser()->getAttribute('fullSession'));   

    $registration->save();
    //$registration->setIsearly($this->getUser()->getAttribute('earlySession');
    
    //just in case ... FLUSH
    //$this->getUser()->getAttributeHolder()->clear();
    
    //2. PREPARE
    
    //CFR
    //conference information
    $conference = ConferencePeer::retrieveByPK($conferenceID);
    
    //CFR - //can push this to template
    $pluralSession = "";
    $pluralSpecialEvent = "";
    
    if (sizeof($selectedSessionIDs)>1)
    {
      $pluralSession = "s";
    }
    
    if (sizeof($selectedSpecialEventIDs)>1)
    {
      $pluralSpecialEvent = "s";
    }    
    
    $paymentDescription = "Payment for LDA America '".$conference->getName()."' conference (".sizeof($selectedSessionIDs)." session".$pluralSession.", ". sizeof($selectedSpecialEventIDs). " special event".$pluralSpecialEvent.')';    
    
    
    $this->paymentDescription = $paymentDescription;
    $this->registrant = $registrant;
    $this->registration = $registration;
    $this->totalConferenceFee = $totalConferenceFee;
    
    $this->getUser()->setAttribute('paymentDescriptionSession',$paymentDescription);
    $this->getUser()->setAttribute('registrationSession', $registration);
    $this->getUser()->setAttribute('registrantSession', $registrant);
    //3. GO!
    
    
  }  

  
  public function handleErrorPaymentProcess()
  {
    $this->forward('publicRegistration', 'startPayment');
  }  
  
  public function executePaymentProcess()
  {
      

    
    $registration = new Registration();
    $registration = $this->getUser()->getAttribute('registrationSession');
    

    
    $registration->setBillFirstName($this->getRequestParameter('bill_first_name'));
    $registration->setBillLastName($this->getRequestParameter('bill_last_name'));
    $registration->setBillAddress($this->getRequestParameter('bill_address'));
    $registration->setBillCity($this->getRequestParameter('bill_city'));
    $registration->setBillState($this->getRequestParameter('bill_state'));
    $registration->setBillZipcode($this->getRequestParameter('bill_zipcode'));
    $registration->setBillCountry($this->getRequestParameter('bill_country'));
    $registration->save();
    
    $creditcard = $this->getRequestParameter('ccNumber');
    $cvs        = $this->getRequestParameter('csvNumber');
    $expiration = $this->getRequestParameter('expiryDate');
    $total      = $registration->getTotalPaid(); 
    
    $invoice    = $registration->getInvoice();
    $description= $this->getUser()->getAttribute('paymentDescriptionSession');
    $registrant = $this->getUser()->getAttribute('registrantSession');
    
    $donatedAmount = 0;  

    $donatedAmount = $this->getRequestParameter('donation');  
    if (is_numeric ($donatedAmount) && $donatedAmount > 0)
    {
      $donation = new Donation();
      $donation->setRegistrationId($registration->getId());
      $donation->setAmount($donatedAmount);
      $donation->save();
      $this->setFlash('donation', $donation);
      $total = $total + $donatedAmount;
    }

    $payment = new Authnet(true); 
    $payment->setTransaction($creditcard, $expiration, $total, $cvs, $invoice); 
    $payment->setParameter('x_invoice_num', $invoice);
    $payment->setParameter('x_description', $description);
    $payment->setParameter('x_first_name', $registration->getBillFirstName());
    $payment->setParameter('x_last_name', $registration->getBillLastName());
    $payment->setParameter('x_address', $registration->getBillAddress());
    $payment->setParameter('x_city', $registration->getBillCity());
    $payment->setParameter('x_state', $registration->getBillState());
    $payment->setParameter('x_zip', $registration->getBillZipcode());
    $payment->setParameter('x_country', $registration->getBillCountry());
    $payment->setParameter('x_email', $registrant->getEmail());
    
    $payment->setParameter('x_ship_to_first_name', $registrant->getName());
    $payment->setParameter('x_ship_to_last_name', "(".$registrant->getMemberId().")");
    $payment->setParameter('x_ship_to_address', $registrant->getAddress());
    $payment->setParameter('x_ship_to_city', $registrant->getCity());
    $payment->setParameter('x_ship_to_state', $registrant->getState());
    $payment->setParameter('x_ship_to_zip', $registrant->getZipcode());
    $payment->setParameter('x_ship_to_country', $registrant->getCountry());
    
    $payment->process(); 
    
    //maybe shld clear this
    //$this->getUser()->getAttributeHolder()->clear();
    
    //approved
    if($payment->getResultResponse() == 1)
    {
      $registration->setPaymentVerified('1');
      $registration->setStatus('complete');
      $registration->save();
    }
    else 
    {
      //payment not approved...
      //1. paymentVerified = 0
      $registration->setPaymentVerified('0');
      $registration->setStatus('complete');
      $registration->save();
      
      // lousy code
      if (is_numeric ($donatedAmount) && $donatedAmount > 0)
      {
        $donation->delete();
      }
      $registration->delete(); //cascade effect will take place here
    }
    
    
    $this->setFlash('payment', $payment);
   
    
    
    return $this->redirect('publicRegistration/receipt');
    
  }  

  public function executeReceipt()
  {
     
    $payment                  = $this->getFlash('payment');

    $this->payment            = $payment;
       
     if($payment->getResultResponse() == 1)
     {
       $registrant               = $this->getUser()->getAttribute('registrantSession');
       
       $this->ccstatus           = $payment->getResultResponseFull() ;
       
       $this->billFirstName      = $payment->getCHFirstName();
       $this->billLastName       = $payment->getCHLastName();
       $this->billAddress        = $payment->getBillingAddress();
       $this->billCity           = $payment->getBillingCity();
       $this->billState          = $payment->getBillingState();
       $this->billZipcode        = $payment->getBillingZip();
       $this->billCountry        = $payment->getBillingCountry();

       
       $this->registrantFirstName= $payment->getShippingFirstName();
       $this->registrantLastName = $payment->getShippingLastName();
       $this->registrantBadge    = $registrant->getBadgename();
       $this->registrantAddress  = $payment->getShippingAddress();
       $this->registrantCity     = $payment->getShippingCity();
       $this->registrantState    = $payment->getShippingState();
       $this->registrantZipcode  = $payment->getShippingZip();
       $this->registrantCountry  = $payment->getShippingCountry();
       $this->registrantEmail    = $payment->getEmail();
       
       $this->invoiceNumber      = $payment->getInvoiceNumber();
       $this->description        = $payment->getDescription();
       $this->confPay            = $payment->getAmount();
       $this->totalPay           = $payment->getAmount();
       $this->donatedAmount      = 0;
       
       
      if ($this->hasFlash('donation'))
      {
        $donation                 = $this->getFlash('donation');
        $this->donatedAmount      = $donation->getAmount();
        $this->confPay            = $payment->getAmount() - $donation->getAmount();
        
      }
     
       
     }

     
  }
  
}


